var express = require('express');
var router = express.Router();
var mysql = require("./tool/mysql");
var url = require('url')
/* GET users listing. */
// 商品列表页
router.get('/', function(req, res, next) {
  mysql.connect(function(db){
    console.log("success-goods");
    var querObj = {};
    var showObj = {
      _id: 0
    };
    // var skipNum = 0;
    // mysql.findSkip(db,"newList",querObj,showObj,skipNum,function(result){
    mysql.find(db,"goods",querObj,showObj,function(result){
      // 拿到数据库数据后返回给页面
        obj ={
          title:"商品列表",
          activeIndex:8,
          isBoss: false,
          result: result,
          sort : 0
        }
        res.render("goods",obj);
        // console.log(result);
        db.close();
    })
  })
});


// 跳转到添加页面
router.get('/addGoods', function(req, res, next) {
  obj ={
    title:"添加产品",
    activeIndex:8,
    isBoss: false,
  }
  res.render("addGoods",obj);
});

// 添加页面中Action表单提交
router.post('/addGoodsAction', function(req, res, next) {
  console.log(req.body); 
  mysql.connect(function(db){
    var insertData = req.body;
    mysql.insert(db,"goods",insertData,function(result){
      res.redirect("/goods");
      // res.send("<script>window.location.href='/goods'</script>");
      db.close();
    })
  }) 
});

// 更新编辑页面
router.get('/updateGoods', function(req, res, next) {
  var id = url.parse(req.url,true).query.id;
  mysql.connect(function(db){
    console.log("success-goods");
    console.log(id)
    var querObj = {id : id};
    var showObj = {
      _id: 0,
    }
    mysql.find(db,"goods",querObj,showObj,function(result){
        obj ={
          title:"更新产品",
          activeIndex:8,
          isBoss: false,
          result: result
        }
        res.render("updateGoods",obj);
        console.log(result);
        db.close();
    })
  })
});

// 更新编辑商品Action表单提交
router.post('/updateGoodsAction', function(req, res, next) {
  console.log(req.body); 
  var updateObj = req.body;
  var whereObj = {
    id: updateObj.id
  }
  mysql.connect(function(db){
    mysql.updateOne(db,"goods",whereObj,updateObj,function(result){
      res.redirect("/goods");
      db.close();
    })
  })
});

// 商品删除事件
router.get('/deleteGoods',function(req, res, next){
  var id = url.parse(req.url,true).query.id;
  obj ={
    title:"删除产品",
    activeIndex:8,
    isBoss: false,
    id :id
  };
  mysql.connect(function(db){
    var deleteObj = {
      id :id
    }
    mysql.deleteOne(db,'goods',deleteObj,function(result){
      res.redirect("/goods");
        db.close();
    })
  })
});

// 按现价排序
router.get('/goodsSort', function(req, res, next) {
  // res.send('产品列表');
  var sortType = url.parse(req.url,true).query.sortType;
  var type = url.parse(req.url,true).query.type;
  mysql.connect(function(db){
    console.log("success-goods");
    var querObj = {};
    var showObj = {
      _id: 0
    };
    var sortObj = {}
    if (sortType == "member_price") {
      sortObj = {
        member_price:type*1 
      }
    }
    mysql.findSort(db,"goods",querObj,showObj,sortObj,function(result){
      // 拿到数据库数据后返回给页面
        obj ={
          title:"商品列表正序",
          activeIndex:8,
          isBoss: false,
          result: result,
          sort:type
        }
        res.render("goods",obj);
        // console.log(result);
        db.close();
    })
  })
});

// 产品分页
router.get('/fenye', function(req, res, next) {
  var pageCode = url.parse(req.url,true).query.pageCode*1;
  var limitNum = url.parse(req.url,true).query.limitNum*1;
  mysql.connect(function(db){
    console.log("success-goods");
    var querObj = {};
    var showObj = {
      _id: 0,
    };
    var skipNum =pageCode*limitNum;
    mysql.find(db,"goods",querObj,showObj,function(resultAll){
      mysql.findFenye(db,"goods",querObj,showObj,limitNum,skipNum,pageCode,function(result){
        var totalPages = Math.ceil(resultAll.length / limitNum);
            obj ={
              title:"商品列表分页",
              activeIndex:8,
              isBoss: false,
              result: result,
              sort : 0,
              allNum: resultAll.length,
              pageCode: pageCode,
              totalPages: totalPages
            }
            res.render("goodsFenye",obj); //页面
            // console.log(result);
            // res.send(result);  //接口
            db.close();
        })
    })
  })
  // res.render("list",{title:"锋影-电影列表",activeIndex:1,isBoss: false});
});


module.exports = router;
